/* eslint-disable */
import axios from "axios";
import md5 from "md5";
import * as qs from "qs";
import api from "../js/apiUrl";
var __assign =
  ( this && this.__assign ) ||
  function () {
    __assign =
      Object.assign ||
      function ( t ) {
        for ( var s, i = 1, n = arguments.length; i < n; i++ ) {
          s = arguments[ i ];
          for ( var p in s ) {
            if ( Object.prototype.hasOwnProperty.call( s, p ) ) {
              t[ p ] = s[ p ];
            }
          }
        }
        return t;
      };
    return __assign.apply( this, arguments );
  };
var MD5KEY = "275c6200656c4f0193a39d1e8a8b0b35";
// var api.JINDOUYUNURL = 'https://newfile.aillo.cc/storage/' // 正式地址
// var api.JINDOUYUNURL = 'https://test-storage-gcp.tcloudapp.com/storage/' // 测试外网
// var api.JINDOUYUNURL = ''
// switch (process.env.NODE_ENV) {
//   case 'development':
//     api.JINDOUYUNURL = 'https://test-storage-gcp.tcloudapp.com/storage/'
//     break
//   case 'test':
//     api.JINDOUYUNURL = 'https://test-storage-gcp.tcloudapp.com/storage/'
//   case 'production':
//     api.JINDOUYUNURL = 'https://newfile.aillo.cc/storage/'
//     break
//   case 'inside': // 内网
//     api.JINDOUYUNURL = 'https://test-storage-gcp.tcloudapp.com/storage/'
//     break
// }
/**
 * 筋斗云SDK对象
 */

// 云存储配置
const USER_CONFIG = {
  accessKey: "aillo_test_a_k",
  secretKey: "aillo_test_s_k",
  bucketId: "1111435842045702146",
  expired: "90"
};

function createUploadToken ( data ) {
  // data {
  //   customId,
  //   expired,
  //   fileName,
  //   fileSize,
  //   fileHash,
  //   customImageInfo 
  // }
  let originalSign =USER_CONFIG.secretKey + "." + USER_CONFIG.bucketId + "." + USER_CONFIG.expired;
  for ( const key in data ) {
    originalSign += "." + data[ key ];
  }

  return USER_CONFIG.accessKey + "." + md5( originalSign );
}

var Jindouyun = /** @class */ ( function () {
  /**
   * 初始化对象
   * @param props
   */
  function Jindouyun ( props ) {
    var userId = props.userId;
    this.userId = userId;
    this.axiosInstance = axios.create( {
      headers: {
        "Content-Type": "multipart/form-data"
      }
    } );
  }
  /**
   * 上传文件的方法
   */
  Jindouyun.prototype.upload = function ( message ) {
    var _this = this;
    var file = message.file;
    var customKey = message.customKey;
    var isPublic = message.isPublic;
    return new Promise( function ( resolve, reject ) {
      var data = new FormData();
      data.append( "file", file );
      // if (customKey) {
      //   data.append("customKey", customKey);
      // }
      // if (isPublic) {
      //   data.append("isPublic", "true");
      // }
      data.append( "expired", USER_CONFIG.expired );
      data.append( "bucketId", USER_CONFIG.bucketId );
      data.append(
        "uploadToken",
        createUploadToken( {
          fileName: file.name,
          fileSize: file.size
        } )
      );
      _this
        .axiosInstance( {
          data: data,
          method: "POST",
          url: api.JINDOUYUNURL + "/uploadSingleFile"
        } )
        .then( function ( result ) {
          result.data.data.fileSize = file.size;
          setTimeout(() => {
            resolve( result );
          }, 800);
        } )
        .catch( function ( err ) {
          reject( err );
        } );
    } );
  };
  /**
   * 上传图片的方法
   */
  Jindouyun.prototype.uploadImg = function ( message ) {
    var _this = this;
    var file = message.file;
    var customKey = message.customKey;
    var isPublic = message.isPublic;
    return new Promise( function ( resolve, reject ) {
      var data = new FormData();
      data.append( "file", file );
      // if (customKey) {
      //   data.append('customKey', customKey)
      // }
      // if (isPublic) {
      //   data.append('isPublic', 'true')
      // }

      data.append( "bucketId", USER_CONFIG.bucketId );
      data.append( "expired", USER_CONFIG.expired );

      data.append(
        "uploadToken",
        createUploadToken( {
          fileName: file.name,
          fileSize: file.size
        } )
      );

      _this
        .axiosInstance( {
          data: data,
          method: "POST",
          url: api.JINDOUYUNURL + "/uploadImageFile"
        } )
        .then( function ( result ) {
          setTimeout(() => {
            result.data.data.fileSize = file.size;
            var img = new Image();
            img.src = _this.getDownloadUrl( result.data.data );
            // console.log('img-src', img.src )
            img.onload = function () {
              result.data.data.width = img.width;
              result.data.data.height = img.height;
              resolve( result );
            };
          }, 1000);
        } )
        .catch( function ( err ) {
          reject( err );
        } );
    } );
  };
  /**
   * 获取下载路径的方法
   */
  // Jindouyun.prototype.getDownloadUrl = function ( data ) {
  //   // console.log('data-go', data)
  //   // console.log('process.env.NODE_ENV-get', process.env.NODE_ENV)
  //   var customKey = data.customKey;
  //   var userFileId = data.fileId;
  //   var isPublic = data.isPublic;
  //   var userId = data.userId;
  //   if ( userFileId && !customKey ) {
  //     if ( isPublic ) {
  //       return (
  //         api.JINDOUYUNURL +
  //         "/download?" +
  //         qs.stringify( {
  //           userId: userId || this.userId,
  //           userFileId: userFileId
  //         } )
  //       );
  //     } else {
  //       var sign = md5( "" + this.userId + userFileId + MD5KEY );
  //       // console.log(55500033)
  //       return (
  //         api.JINDOUYUNURL +
  //         "/download?" +
  //         qs.stringify( {
  //           userId: userId || this.userId,
  //           userFileId: userFileId,
  //           sign: sign
  //         } )
  //       );
  //     }
  //   } else if ( !userFileId && customKey ) {
  //     if ( isPublic ) {
  //       return (
  //         api.JINDOUYUNURL +
  //         "/download?" +
  //         qs.stringify( { userId: userId || this.userId, customKey: customKey } )
  //       );
  //     } else {
  //       var sign = md5( "" + this.userId + customKey + MD5KEY );
  //       return (
  //         api.JINDOUYUNURL +
  //         "/download?" +
  //         qs.stringify( {
  //           userId: userId || this.userId,
  //           customKey: customKey,
  //           sign: sign
  //         } )
  //       );
  //     }
  //   } else if ( userFileId && customKey ) {
  //     if ( isPublic ) {
  //       return (
  //         api.JINDOUYUNURL +
  //         "/download?" +
  //         qs.stringify( {
  //           userId: userId || this.userId,
  //           customKey: customKey,
  //           userFileId: userFileId
  //         } )
  //       );
  //     } else {
  //       var sign = md5( "" + this.userId + customKey + userFileId + MD5KEY );
  //       return (
  //         api.JINDOUYUNURL +
  //         "/download?" +
  //         qs.stringify( {
  //           userId: userId || this.userId,
  //           customKey: customKey,
  //           userFileId: userFileId,
  //           sign: sign
  //         } )
  //       );
  //     }
  //   } else {
  //     const id = data.id ;
  //     const originalSign = `${USER_CONFIG.secretKey}.${id}.${USER_CONFIG.bucketId}.attachment`;
  //     const downloadToken = `${USER_CONFIG.accessKey}.${md5( originalSign )}`;
  //     const downloadUrl = `${api.JINDOUYUNURL}/getCustomImage?bucketId=${USER_CONFIG.bucketId}&userFileId=${id}&downloadToken=${downloadToken}&contentDisposition=attachment`;
  //     console.log( downloadUrl );
  //     return downloadUrl;
  //   }
  // };
  Jindouyun.prototype.getDownloadUrl = function (data){
     const id = data.id || data.fileId ;
     const originalSign = `${USER_CONFIG.secretKey}.${id}.${USER_CONFIG.bucketId}.attachment`;
     const downloadToken = `${USER_CONFIG.accessKey}.${md5( originalSign )}`;
     const downloadUrl = `${api.JINDOUYUNURL}/getCustomImage?bucketId=${USER_CONFIG.bucketId}&userFileId=${id}&downloadToken=${downloadToken}&contentDisposition=attachment`;
     console.log( downloadUrl );
     return downloadUrl;
  }



  /**
   * 获取文件的方法
   */
  Jindouyun.prototype.uploadFile = function ( data ) {
    var _this = this;
    var customKey = data.customKey;
    var fileId = data.fileId;
    var isPublic = data.isPublic;
    var userId = data.userId;
    if ( !data.userId ) {
      data.userId = this.userId;
    }
    return new Promise( function ( resolve, reject ) {
      _this
        .axiosInstance( {
          url: _this.getDownloadUrl( data ),
          method: "GET"
        } )
        .then( function ( result ) {
          resolve( result );
        } )
        .catch( function ( err ) {
          reject( err );
        } );
    } );
  };
  /**
   * 获取自定义图片
   */
  // Jindouyun.prototype.downloadCustomFile = function ( data ) {
  //   // console.log('process.env.NODE_ENV', process.env.NODE_ENV)
  //   // var userId = data.userId; var customKey = data.customKey; var fileId = data.fileId;
  //   // var sign = data.sign; var width = data.width; var height = data.height; var quality = data.quality;
  //   // var rotate = data.rotate; var scale = data.scale; var isPublic = data.isPublic
  //   const {
  //     userId,
  //     customKey,
  //     fileId,
  //     sign,
  //     width,
  //     height,
  //     quality,
  //     rotate,
  //     scale,
  //     isPublic
  //   } = data;
  //   var temp = {
  //     userId: userId,
  //     customKey: customKey,
  //     fileId: fileId,
  //     isPublic: isPublic
  //   };
  //   var url = this.getDownloadUrl( temp );
  //   // console.log('api.JINDOUYUNURL', api.JINDOUYUNURL)
  //   // console.log('urlllll', url)
  //   if ( url ) {
  //     // console.log(555)
  //     var parme = qs.parse( url.split( "?" )[ 1 ] );
  //     // console.log('张www', api.JINDOUYUNURL + '/getCustomImage?' + qs.stringify(__assign({}, parme, { width: width, height: height, quality: quality, rotate: rotate, scale: scale })))
  //     return (
  //       api.JINDOUYUNURL +
  //       "/getCustomImage?" +
  //       qs.stringify(
  //         __assign( {}, parme, { width, height, quality, rotate, scale } )
  //       )
  //     );
  //   } else {
  //     return null;
  //   }
  // };

  Jindouyun.prototype.downloadCustomFile = function(data){
    // data = {
    //   userFileId:'',
    //   quality:"0.3"
    // }

    const params = {
      bucketId:USER_CONFIG.bucketId,
      userFileId:data.fileId,
      expired:90,
      downloadToken:'',
      quality:data.quality || 0.3,
      contentDisposition:'attachment'
    }

    const originalSign = `${USER_CONFIG.secretKey}.${params.userFileId}.${params.bucketId}.${params.contentDisposition}`
    const downloadToken = USER_CONFIG.accessKey + '.' + md5(originalSign)
    params.downloadToken = downloadToken

    return `${api.JINDOUYUNURL}/getCustomImage?${qs.stringify(params)}`
  }
  /**
   * 文件恢复的方法
   */
  Jindouyun.prototype.recoverFile = function ( data ) {
    var _this = this;
    return new Promise( function ( resolve, reject ) {
      _this
        .axiosInstance( {
          url: api.JINDOUYUNURL + "/userFileId",
          method: "POST"
        } )
        .then( function ( result ) {
          resolve( result );
        } )
        .catch( function ( err ) {
          reject( err );
        } );
    } );
  };
  /**
   * 删除接口的方法
   */
  Jindouyun.prototype.deleteFile = function ( data ) {
    var _this = this;
    return new Promise( function ( resolve, reject ) {
      var axiosJson = axios.create( {
        headers: {
          "Content-Type": "application/json",
          "User-Id": _this.userId
        }
      } );
      axiosJson( {
        url: api.JINDOUYUNURL + "/operation/delete",
        method: "POST",
        data: { userFileId: data }
      } )
        .then( function ( result ) {
          resolve( result );
        } )
        .catch( function ( err ) {
          reject( err );
        } );
    } );
  };
  return Jindouyun;
} )();
export default Jindouyun;
